package cn.suancloud.aop;


import cn.suancloud.bean.DBContextHolder;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataSourceAop {


    @Pointcut("!@annotation(cn.suancloud.annotation.Master) " +
            "&& (execution(* cn.suancloud.service..*.select*(..)) " +
            "|| execution(* cn.suancloud.service..*.get*(..)))")
    public void readPointCut(){}



    @Pointcut("@annotation(cn.suancloud.annotation.Master) " +
            "|| execution(* cn.suancloud.service..*.insert*(..)) " +
            "|| execution(* cn.suancloud.service..*.add*(..)) " +
            "|| execution(* cn.suancloud.service..*.update*(..)) " +
            "|| execution(* cn.suancloud.service..*.edit*(..)) " +
            "|| execution(* cn.suancloud.service..*.delete*(..)) " +
            "|| execution(* cn.suancloud.service..*.remove*(..))")
    public void writePointCut() { }


    @Before("readPointCut()")
    public void read(){
        DBContextHolder.slave();
    }

    @Before("writePointCut()")
    public void write() {
        DBContextHolder.master();
    }


}
